package com.simpleplayer.engine;

import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.view.Surface;
import android.view.SurfaceHolder;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Player;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

class IjkPlayerEngine extends PlayerEngine {

    private final Context mContext;
    private IjkMediaPlayer mPlayer;

    public IjkPlayerEngine(Context context) {
        this.mContext = context;
        ensurePlayerCreated();
    }

    private void ensurePlayerCreated() {
        if(mPlayer == null) {
            mPlayer = new IjkMediaPlayer();

            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "opensles", 1);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "overlay-format", IjkMediaPlayer.SDL_FCC_RV32);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 1);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "http-detect-range-support", 1);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_CODEC, "min-frames", 100);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "enable-accurate-seek", 1);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 1);
            mPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-handle-resolution-change", 1);
            mPlayer.setVolume(1.0f, 1.0f);

            IjkPlayerListener listener = new IjkPlayerListener();
            mPlayer.setOnBufferingUpdateListener(listener);
            mPlayer.setOnCompletionListener(listener);
            mPlayer.setOnErrorListener(listener);
            mPlayer.setOnPreparedListener(listener);
            mPlayer.setOnSeekCompleteListener(listener);
            mPlayer.setOnVideoSizeChangedListener(listener);
            mPlayer.setOnInfoListener(listener);
        }
    }

    private IjkMediaPlayer getPlayer() {
        ensurePlayerCreated();
        return mPlayer;
    }

    @Override
    public long getCurrentPosition() {
        return getPlayer().getCurrentPosition();
    }

    @Override
    public long getDuration() {
        return getPlayer().getDuration();
    }

    @Override
    public void prepare(String uri) {
        try {
            getPlayer().setDataSource(mContext, Uri.parse(uri));
            getPlayer().setAudioStreamType(AudioManager.STREAM_MUSIC);
            getPlayer().prepareAsync();
        } catch (Exception ex) {
            ex.printStackTrace();
            getClient().onError();
        }

    }

    @Override
    public void play() {
        getPlayer().start();
    }

    @Override
    public void pause() {
        getPlayer().pause();
    }

    @Override
    public void release() {
        if (mPlayer != null) {
            mPlayer.reset();
            mPlayer.release();
            mPlayer = null;
        }
    }

    @Override
    public void seekTo(long position) {
        getPlayer().seekTo(position);
    }

    @Override
    public void setSurface(Surface surface) {
        getPlayer().setSurface(surface);
    }

    @Override
    public void setDisplay(SurfaceHolder holder) {
        getPlayer().setDisplay(holder);
    }

    @Override
    public int getVideoHeight() {
        return 0;
    }

    @Override
    public int getVideoWidth() {
        return 0;
    }

    @Override
    public void setVolume(float volume) {
        getPlayer().setVolume(volume, 1);
    }

    private class IjkPlayerListener implements
            IMediaPlayer.OnPreparedListener,
            IMediaPlayer.OnCompletionListener,
            IMediaPlayer.OnSeekCompleteListener,
            IMediaPlayer.OnErrorListener,
            IMediaPlayer.OnBufferingUpdateListener,
            IMediaPlayer.OnVideoSizeChangedListener,
            IMediaPlayer.OnInfoListener {

        @Override
        public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int i) {
            // 缓冲比例，用于做第二进度条
        }

        @Override
        public void onCompletion(IMediaPlayer iMediaPlayer) {
            getClient().onPlaybackComplete();
        }

        @Override
        public boolean onError(IMediaPlayer iMediaPlayer, int i, int i1) {
            getClient().onError();
            return true;
        }

        @Override
        public void onPrepared(IMediaPlayer iMediaPlayer) {
            getClient().onPrepared();
        }

        @Override
        public void onSeekComplete(IMediaPlayer iMediaPlayer) {
            getClient().onSeekComplete();
        }

        @Override
        public void onVideoSizeChanged(IMediaPlayer iMediaPlayer, int width, int height, int sar_num, int sar_den) {
            getClient().onVideoSizeChanged(width, height);
        }

        @Override
        public boolean onInfo(IMediaPlayer iMediaPlayer, int what, int extra) {
            return false;
        }
    }
}
